using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

public class UIBase : UIBehaviour
{
    private List<Button> buttonList = new List<Button>();
    private List<Toggle> toggleList = new List<Toggle>();
    private List<InputField> inputFieldList = new List<InputField>();
    
    private CanvasGroup uiMask;
    private CanvasGroup canvasGroup;
    protected Transform uiContent;
    
    // 初始化基础组件
    private void InitializeBaseComponent()
    {
        uiMask = transform.Find("UIMask").GetComponent<CanvasGroup>();
        canvasGroup = transform.GetComponent<CanvasGroup>();
        uiContent = transform.Find("UIContent").transform;
    }

    #region 事件管理

    public void AddButtonClickListener(Button button, UnityAction action)
    {
        if (button != null)
        {
            if (!buttonList.Contains(button))
            {
                buttonList.Add(button);
            }
            button.onClick.RemoveAllListeners();
            button.onClick.AddListener(action);
        }
    }

    public void AddToggleClickListener(Toggle toggle, UnityAction<bool, Toggle> action)
    {
        if (toggle != null)
        {
            if (!toggleList.Contains(toggle))
            {
                toggleList.Add(toggle);
            }
            toggle.onValueChanged.RemoveAllListeners();
            toggle.onValueChanged.AddListener((isOn) =>
            {
                action?.Invoke(isOn, toggle);
            });
        }
    }

    public void AddInputFieldClickListener(InputField inputField, UnityAction<string> onChangeAction, UnityAction<string> endAction)
    {
        if (inputField != null)
        {
            if (!inputFieldList.Contains(inputField))
            {
                inputFieldList.Add(inputField);
            }
            inputField.onValueChanged.RemoveAllListeners();
            inputField.onEndEdit.RemoveAllListeners();
            inputField.onValueChanged.AddListener(onChangeAction);
            inputField.onEndEdit.AddListener(endAction);
        }
    }

    public void RemoveAllButtonListeners()
    {
        foreach (var item in buttonList)
        {
            item.onClick.RemoveAllListeners();
        }
    }

    public void RemoveAllToggleListeners()
    {
        foreach (var item in toggleList)
        {
            item.onValueChanged.RemoveAllListeners();
        }
    }

    public void RemoveAllInputFieldListeners()
    {
        foreach (var item in inputFieldList)
        {
            item.onValueChanged.RemoveAllListeners();
            item.onEndEdit.RemoveAllListeners();
        }
    }

    #endregion

    public override void SetVisible(bool visable)
    {
        gameObject.SetActive(Visible);
    }
    
    public void SetMaskVisible(bool isVisble)
    {
        if (!UISetting.Instance.SINGMASK_SYSTEM)
        {
            return;
        }
        uiMask.alpha = isVisble ? 1 : 0;
    }
    
    #region 生命周期

    public override void OnCreate()
    {
        base.OnCreate();
    }

    public override void OnDestroy()
    {
        base.OnDestroy();
        RemoveAllButtonListeners();
        RemoveAllToggleListeners();
        RemoveAllInputFieldListeners();
        buttonList.Clear();
        toggleList.Clear();
        inputFieldList.Clear();
    }

    public override void OnEnable()
    {
        base.OnEnable();
    }

    public override void OnDisable()
    {
        base.OnDisable();
    }

    public override void Update()
    {
        base.Update();
    }

    #endregion
}
